data manipulations in sql

Inserting data:

 

Having now built the structure of the database it is time to populate the tables with some data.
The command to add new records to a table (usually referred to as an append query), is:

INSERT INTO target [(field1[, field2[, ...]])]
VALUES (value1[, value2[, ...]);

For example, to add a User record for user Jim Jones, we would issue the following INSERT query:

INSERT INTO User (FirstName, LastName, UserID, Dept, EmpNo, PCType) 6
VALUES ("Jim", "Jones", "Jjones","Finance", 9, "DellDimR450");

The INSERT command can also be used to copy data from one table into another. For example, The SQL query to perform this is:

INSERT INTO User ( FirstName, LastName, UserID, Dept, EmpNo, PCType, Internet )
SELECT FirstName, LastName, UserID, Dept, EmpNo, PCType, Internet
FROM NewUsers;

Updating data

The INSERT command is used to add records to a table, but what if you need to make an amendment to a particular record? In this case the SQL command to perform updates is the UPDATE command, with syntax:

UPDATE table
SET newvalue
WHERE criteria;

For example, let's assume that we want to move user Jim Jones from the Finance department to Marketing. Our SQL statement would then be:

UPDATE User
SET Dept="Marketing"
WHERE EmpNo=9;

You can also use the SET keyword to perform arithmetical or logical operations on the values. For example if you have a table of salaries and you want to give everybody a 10% increase you can issue the following command:
UPDATE PayRoll
SET Salary=Salary * 1.1;

 

Deleting data:

 

SQL provides a simple command to delete complete records. The syntax of the command is:

 

DELETE [table.*]
FROM table
WHERE criteria;


Let's assume we have a user record for John Doe, (with an employee number of 99), which we want to remove from our User we could issue the following query:


DELETE *
FROM User
WHERE EmpNo=99;

 

 

 

 

DBMS & SQl by P. Muthulakshmi & v. vanthana